﻿
Imports std
Imports elpmis
Imports elpmis.internalFunctions

Namespace lpmsservFunctions

    Public Module lpmsservConfigShared
        Public Function config(ByVal baseconfig As configParser) As configParser
            If baseconfig Is Nothing Then
                Return lpmsserv.config
            Else
                Return baseconfig
            End If
        End Function

        Public Function section(ByVal basesection As String) As String
            If isemptystring(basesection) Then
                Return configNaming.lpmsserv.name
            Else
                Return basesection
            End If
        End Function

        Public Function filters(ByVal basefilters As map(Of String, String), ByVal ctx As context, _
                                Optional ByVal para_useHttpContextFilters As String = "useHttpContextFilters") _
                                As map(Of String, String)
            Dim useHttpContextFilters As Boolean = True
            ctx.findVariableBool(para_useHttpContextFilters, useHttpContextFilters)

            If useHttpContextFilters Then
                If basefilters Is Nothing Then
                    basefilters = New map(Of String, String)()
                End If
                Dim httpfilters As map(Of String, String) = Nothing
                httpfilters = currentContext().filters()
                Dim it As map(Of String, String).iterator = Nothing
                it = httpfilters.begin()
                While it <> httpfilters.end()
                    basefilters((+it).first()) = (+it).second
                    it += 1
                End While
            End If

            Return basefilters
        End Function
    End Module

    'parameters, same as configreader
    '     extra, useHttpContextFilters, any, optional, use as bool, use httpContext.filter(), default value is true
    '            section, default value is lpmsserv.configNaming.lpmsserv.name
    '            config, default value is lpmsserv.config
    'return, same as configreader
    Public Class configreader2
        Inherits configreader

        Public Shadows Class parameters
            Public Const useHttpContextFilters As String = "useHttpContextFilters"
        End Class

        Public Sub New()
            MyBase.New("lpmsserv.config.read")
        End Sub

        Protected Overrides Function section(ByVal ctx As context) As String
            Return lpmsservConfigShared.section(MyBase.section(ctx))
        End Function

        Protected Overrides Function config(ByVal ctx As context) As configParser
            Return lpmsservConfigShared.config(MyBase.config(ctx))
        End Function

        Protected Overrides Function filters(ByVal ctx As context) As map(Of String, String)
            Return lpmsservConfigShared.filters(MyBase.filters(ctx), ctx, parameters.useHttpContextFilters)
        End Function
    End Class

    'same as configlistreader & configreader2
    Public Class configlistreader2
        Inherits configlistreader

        Public Shadows Class parameters
            Public Const useHttpContextFilters As String = "useHttpContextFilters"
        End Class

        Public Sub New()
            MyBase.New("lpmsserv.config.list")
        End Sub

        Protected Overrides Function section(ByVal ctx As context) As String
            Return lpmsservConfigShared.section(MyBase.section(ctx))
        End Function

        Protected Overrides Function config(ByVal ctx As context) As configParser
            Return lpmsservConfigShared.config(MyBase.config(ctx))
        End Function

        Protected Overrides Function filters(ByVal ctx As context) As map(Of String, String)
            Return lpmsservConfigShared.filters(MyBase.filters(ctx), ctx, parameters.useHttpContextFilters)
        End Function
    End Class
End Namespace